﻿using System.Collections.Generic;
using System.Xml;
using SdmxMl.Helper;
using SdmxMl.Common;

namespace SdmxMl.Structure
{
    public class BaseSetlistRef : ArtefactRefBase
    {
 
        #region Properties
        protected string _idTag;

        #endregion //Properties

        #region Constructors

        public BaseSetlistRef()
        {
        }

        protected void ReadXml(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr, string tag)
        {
            _idTag = tag;

            if (nsmgr.VersionSdmx == SdmxVersion.SDMX_20)
            {
                Urn = Xmlhelper.GetNodeText(nodeArtefact, "./structure:URN", nsmgr);
                AgencyId = Xmlhelper.GetNodeText(nodeArtefact, "./structure:AgencyID", nsmgr);
                // Id according to structureSet element type
                Id = Xmlhelper.GetNodeText(nodeArtefact, "./structure:" + _idTag, nsmgr);
                Version = Xmlhelper.GetNodeText(nodeArtefact, "./structure:Version", nsmgr);
            }
            else
            {
                XmlNode ndCoreRef = nodeArtefact.SelectSingleNode("./Ref", nsmgr);
                if (ndCoreRef != null)
                {
                    AgencyId = Xmlhelper.GetAtt(ndCoreRef, "agencyID");
                    Id = Xmlhelper.GetAtt(ndCoreRef, "id");
                    Version = Xmlhelper.GetAtt(ndCoreRef, "version");
                }
            }
        }

        #endregion // Constructors

        #region XML Representation
        
        public virtual void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            xw.WriteStartElement(prefix, tag, null);

            Xmlhelper.WriteElementIfExist(xw, prefix, "URN", Urn);
            Xmlhelper.WriteElementIfExist(xw, prefix, "AgencyID", AgencyId);
            Xmlhelper.WriteElementIfExist(xw, prefix, _idTag, Id);
                if (Version != "1.0")
                    Xmlhelper.WriteElementIfExist(xw, prefix, "Version", Version);
            xw.WriteEndElement();
        }
 
        #endregion // XML Representation

        #region String Representation

        public override string ToString()
        {
            return Id;
        }
        #endregion
    }

    /// <summary>
    /// List of references
    /// </summary>
    public class BaseSetlistRefList : List<BaseSetlistRef>
    {
    }
}
